﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Cryptography;
using System.Text;

namespace Beeldbankmvc.Code
{
    public class Encrypt
    {
        private static byte[] lbtVector = { 240, 3, 45, 29, 0, 76, 173, 59 };
        private static string lscryptoKey = "Ancient Chinese secret, huh";

        public static string psDecrypt(string sQueryString)
        {
            byte[] buffer;
            TripleDESCryptoServiceProvider loCryptoClass = new TripleDESCryptoServiceProvider();
            MD5CryptoServiceProvider loCryptoProvider = new MD5CryptoServiceProvider();

            try
            {
                // changed base64 decode to UrlTokenDecode (base64 url safe)
                buffer = System.Web.HttpServerUtility.UrlTokenDecode(sQueryString);
                loCryptoClass.Key = loCryptoProvider.ComputeHash(ASCIIEncoding.ASCII.GetBytes(lscryptoKey));
                loCryptoClass.IV = lbtVector;
                return Encoding.ASCII.GetString(loCryptoClass.CreateDecryptor().TransformFinalBlock(buffer, 0, buffer.Length));
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                loCryptoClass.Clear();
                loCryptoProvider.Clear();
                loCryptoClass = null;
                loCryptoProvider = null;

            }
        }


        //Author      :       Nikhil Gupta
        //Description :       This function encrypts a given string
        //Parameters  :       String
        //Return Values:      Encrypted String
        //Called From :       Business Layer
        public static string psEncrypt(string sInputVal)
        {
            TripleDESCryptoServiceProvider loCryptoClass = new TripleDESCryptoServiceProvider();
            MD5CryptoServiceProvider loCryptoProvider = new MD5CryptoServiceProvider();
            byte[] lbtBuffer;

            try
            {
                lbtBuffer = System.Text.Encoding.ASCII.GetBytes(sInputVal);
                loCryptoClass.Key = loCryptoProvider.ComputeHash(ASCIIEncoding.ASCII.GetBytes(lscryptoKey));
                loCryptoClass.IV = lbtVector;
                // changed base64 decode to UrlTokenDecode (base64 url safe)
                sInputVal = System.Web.HttpServerUtility.UrlTokenEncode(loCryptoClass.CreateEncryptor().TransformFinalBlock(lbtBuffer, 0, lbtBuffer.Length));
                return sInputVal;
            }
            catch (CryptographicException ex)
            {
                throw ex;
            }
            catch (FormatException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                loCryptoClass.Clear();
                loCryptoProvider.Clear();
                loCryptoClass = null;
                loCryptoProvider = null;
            }
        }
    }
}